package cn.ysym.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/14 0014.
 */

abstract public class DaoBase {
    private static SQLiteDatabase sqLiteDatabase;

    public DaoBase(Context context) {
        if (sqLiteDatabase == null) {
            DatabaseHelper dh = new DatabaseHelper(context);
            sqLiteDatabase = dh.getWritableDatabase();
        }
    }

    public void execSql(String sql, Object... bindArgs) {
        sqLiteDatabase.execSQL(sql, bindArgs);
    }

    public List<String[]> query(int maxRow, String sql, String... bindArgs) {
        List<String[]> ss = new ArrayList<>();
        Cursor c = sqLiteDatabase.rawQuery(sql, bindArgs);
        int rowIndex = 0;
        while (c.moveToNext() && rowIndex < maxRow) {
            String[] vals = new String[c.getColumnCount()];
            for (int j = 0; j < c.getColumnCount(); j++) {
                vals[j] = c.getString(j);
            }
            ss.add(vals);
            rowIndex++;
        }
        return ss;
    }

}
